/**
 *@auther: tang
 *@Date: 2021-02-22
 *Project: p2pPlatformSys
 *Lang: TypeScript
 */
import { Service } from 'egg';
// import { stringify } from 'querystring';
import * as utils from '../utils/utils';
const { Op } = require('sequelize');

class commonpostService extends Service {
  async query(params: any, modelStr: string, related: Array<any>, state: string) {
    const model = utils.upperFirst(modelStr);
    console.log('查询');
    const { ctx } = this;
    const page = params.page || 1,
      size = params.size || 10;
    // const oriData:any = utils.clearNullItems(params);
    // 根据传入的关键字生成查询数组
    const genQuery = utils.generateQueryArr(utils.clearNullItems(params.keywords)) || {};
    const queryObj = {
      [Op.and]: [
        genQuery,
        {
          [state]: {
            [Op.or]: String(params[state]).split(','),
          },
        },
      ],
    };
    let genInclud:Array<any> = [];
    console.log(222);
    if (modelStr && related.length > 0) {
      console.log('生成关联', related, utils.upperFirst(model));
      // 生成一对多关联
      related.forEach(item => {
        const rmodel = item.model;
        // console.log(ctx.model[rmodel]);
        // console.log(item, ctx.model[model], ctx.model.RechargerecordType);
        ctx.model[model].belongsTo(
          ctx.model[rmodel],
          { foreignKey: item.foreignKey });
      });
      console.log(related);
      genInclud = related.map(item => {
        const rmodel = item.model;
        if (item.attributes) {
          return { model: ctx.model[rmodel], attributes: item.attributes };
        }
        return { model: ctx.model[rmodel] };
      });
      console.log(genInclud);
    }
    console.log('参数', genInclud, queryObj, model);
    const res: object = await ctx.model[model].findAndCountAll({
      where: queryObj,
      include: genInclud,
      limit: Number(size),
      offset: (page - 1) * size,
    }).catch(err => {
      console.log(err);
    });
    console.log(res);
    return {
      res,
    };
  }
  async update(params:any, modelStr:string) {
    const { ctx } = this;
    const bid = params.id;
    const model = utils.upperFirst(modelStr);
    const oriData:any = utils.clearNullItems(params) || {};
    console.log(oriData);
    const res:any = await ctx.model[model].update({ ...oriData }, {
      where: {
        id: bid,
      },
    }).catch(err => {
      console.log(err);
    });
    console.log(res);
    return {
      ...res,
      id: bid,
      state: params.state || -1,
    };
  }
  async add(params:any, modelStr:string) {
    // return allBits;
    const { ctx } = this;
    const model = utils.upperFirst(modelStr);
    console.log(model);
    const oriData: any = utils.clearObj((await ctx.model[model].findAll({ limit: 1 }))[0] ? (await ctx.model[model].findAll({ limit: 1 }))[0].dataValues : {});
    console.log(94, params);
    let prams = {};
    if (oriData) {
      prams = utils.generateObjArr(params, oriData);
    } else {
      prams = params;
    }
    console.log(111111);
    const res:object[] = await ctx.model[model].create(prams).catch(err => {
      console.log(err);
    });
    return res;
  }
  async delete(params:any, modelStr:string) {
    // return allBits;
    const { ctx } = this;
    const model = utils.upperFirst(modelStr);
    const res: object[] = await ctx.model[model].destroy({
      where: {
        id: params.id,
      },
    });
    return res;
  }
}
module.exports = commonpostService;
